import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:trollee/app/core/core.dart';

enum CustomButtonSize { big, small, minimum }

class CustomButton extends StatelessWidget {
  const CustomButton({
    required this.text,
    required this.onPressed,
    Key? key,
    this.margin,
    this.width,
    this.height,
    this.disable = false,
    this.full = true,
    this.borderWidth = 2,
    this.borderColor,
    this.textColor,
    this.maxLines,
    this.buttonSize = CustomButtonSize.big,
    this.minWidth,
    this.color = AppColors.background_color_primary,
    this.iconData,
    this.iconSize,
    this.iconPadding,
    this.withBorderByFull = false,
  }) : super(key: key);

  const CustomButton.minimum({
    required this.text,
    required this.onPressed,
    Key? key,
    this.margin,
    this.width,
    this.height,
    this.disable = false,
    this.full = true,
    this.borderWidth = 2,
    this.borderColor,
    this.textColor,
    this.maxLines,
    this.minWidth,
    this.color = AppColors.background_color_primary,
    this.iconData,
    this.iconSize,
    this.iconPadding,
    this.withBorderByFull = false,
  })  : buttonSize = CustomButtonSize.minimum,
        super(key: key);

  const CustomButton.small({
    required this.text,
    required this.onPressed,
    Key? key,
    this.margin,
    this.width,
    this.height,
    this.disable = false,
    this.full = true,
    this.borderWidth = 2,
    this.borderColor,
    this.textColor,
    this.maxLines,
    this.minWidth,
    this.color = AppColors.background_color_primary,
    this.iconData,
    this.iconSize,
    this.iconPadding,
    this.withBorderByFull = false,
  })  : buttonSize = CustomButtonSize.small,
        super(key: key);

  const CustomButton.big({
    required this.text,
    required this.onPressed,
    Key? key,
    this.margin,
    this.width,
    this.height,
    this.disable = false,
    this.full = true,
    this.borderWidth = 2,
    this.borderColor,
    this.textColor,
    this.maxLines,
    this.minWidth,
    this.color = AppColors.background_color_primary,
    this.iconData,
    this.iconSize,
    this.iconPadding,
    this.withBorderByFull = false,
  })  : buttonSize = CustomButtonSize.big,
        super(key: key);

  final String text;
  final EdgeInsetsGeometry? margin;
  final double? width;
  final double? minWidth;
  final double? height;
  final VoidCallback onPressed;
  final bool disable;
  final CustomButtonSize buttonSize;

  /// 是否为填充按钮，false为边框按钮
  final bool full;
  final double borderWidth;
  final Color? borderColor;
  final Color? textColor;
  final Color color;
  final bool withBorderByFull;

  final int? maxLines;

  final IconData? iconData;
  final double? iconSize;
  final double? iconPadding;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width ?? (buttonSize == CustomButtonSize.big ? double.infinity : null),
      height: height,
      margin: margin,
      constraints: minWidth != null ? BoxConstraints(minWidth: minWidth!) : null,
      child: TextButton(
        onPressed: disable ? null : onPressed,
        style: ButtonStyle(
          backgroundColor: MaterialStateProperty.resolveWith((states) {
            if (states.contains(MaterialState.disabled)) {
              return full ? AppColors.background_color_disable : null;
            }
            return full ? color : null;
          }),
          //设置水波纹颜色
          overlayColor: MaterialStateProperty.resolveWith((states) {
            if (states.contains(MaterialState.disabled)) {
              return null;
            }
            return AppColors.primary_color2.withAlpha(full ? 100 : 20);
          }),
          shape: MaterialStateProperty.resolveWith((states) {
            final border = BorderSide(
              width: borderWidth.w,
              color: states.contains(MaterialState.disabled) ? AppColors.background_color_disable : borderColor ?? AppColors.background_color_primary,
            );
            return RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(AppDimens.dp6),
              side: borderWidth == 0
                  ? BorderSide.none
                  : full
                      ? (withBorderByFull ? border : BorderSide.none)
                      : border,
            );
          }),
          padding: height == null ? MaterialStateProperty.all(EdgeInsets.symmetric(
            vertical: buttonSize == CustomButtonSize.big
                ? AppDimens.dp16
                : buttonSize == CustomButtonSize.small
                    ? AppDimens.dp10
                    : AppDimens.dp6,
            horizontal: buttonSize == CustomButtonSize.big ? AppDimens.dp24 : AppDimens.dp16,
          )) : null,
        ),
        child: iconData == null
            ? Text(
                text,
                style: (buttonSize == CustomButtonSize.big
                        ? AppTextStyles.text_16_bold_primary
                        : buttonSize == CustomButtonSize.small
                            ? AppTextStyles.body1_14_Bold_primary
                            : AppTextStyles.body2_12_Bold_primary)
                    .copyWith(
                  color:
                      full ? textColor ?? AppColors.font_color_while : (disable ? AppColors.background_color_disable : textColor ?? AppColors.primary_color1),
                  height: 1.2,
                ),
                maxLines: maxLines,
                overflow: maxLines == null ? null : TextOverflow.ellipsis,
              )
            : Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Icon(
                    iconData,
                    size: iconSize,
                    color:
                        full ? textColor ?? AppColors.font_color_while : (disable ? AppColors.background_color_disable : textColor ?? AppColors.primary_color1),
                  ),
                  SizedBox(
                    width: iconPadding ?? AppDimens.dp8,
                  ),
                  Text(
                    text,
                    style: (buttonSize == CustomButtonSize.big
                            ? AppTextStyles.text_16_bold_primary
                            : buttonSize == CustomButtonSize.small
                                ? AppTextStyles.body1_14_Bold_primary
                                : AppTextStyles.body2_12_Bold_primary)
                        .copyWith(
                      color: full
                          ? textColor ?? AppColors.font_color_while
                          : (disable ? AppColors.background_color_disable : textColor ?? AppColors.primary_color1),
                      height: 1.2,
                    ),
                  ),
                ],
              ),
      ),
    );
  }
}
